//
//  ST_SelectDailyMomentController.swift
//  btvideo
//
//  Created by mac on 2020/7/22.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SwiftyUserDefaults
import MJRefresh

/**
 发布日常 选择分类
 */
class ST_SelectDailyMomentController: ST_BTLightBarViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Color.white
        
        if let str = self.title{
            title_nav_btn.setTitleForAllStates(str)
        }
        
        
         
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.top.equalTo(own_navigation_bar.snp.bottom)
        }
        
        
        self.request_data()
        
    }
    
    fileprivate var currentPage = 0
    
    fileprivate func request_data(){
        
       
        JVrequest(PH.fetchDailyBaseListApi, success: { (json) in
            
            
            self.data_array = json["data"].arrayValue.map({ (itemJson) -> ST_DailBaseModel in
                return ST_DailBaseModel.init(fromJson: itemJson)
            })
            
            self.collectionView.reloadData()
            
            
        }) { (code, err) in
            
            ToastHud(text: err, view: self.view)
        }
        
    }
    
    //     MARK: - Properties
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
        let controlLayout = UICollectionViewFlowLayout()
        controlLayout.minimumLineSpacing = 22
        controlLayout.minimumInteritemSpacing = 10
        controlLayout.sectionInset = UIEdgeInsets.init(top: 15, left: 15, bottom: BTSafeAreaBottom + 79, right: 15)
        
        controlLayout.itemSize = CGSize.init(width: SelectST_DailyMomentCell.width, height: SelectST_DailyMomentCell.height)
        
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self
        control.register(cellWithClass: SelectST_DailyMomentCell.self)
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.backgroundColor = Color.white
        
        control.alwaysBounceVertical = true
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        control.allowsMultipleSelection = true
        return control
    }()
    
    fileprivate var data_array = [ST_DailBaseModel]()
    
}

extension ST_SelectDailyMomentController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    // MARK: UICollectionViewDataSource
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: SelectST_DailyMomentCell.self, for: indexPath)
        
        cell.model = data_array[indexPath.item]
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
        if self.title?.contains("日常看板") ?? false{
            
            let vc = ST_SomeOneDailyViewController()
            vc.dailyModel = data_array[safe:indexPath.row]
            self.navigationController?.pushViewController(vc)
            
            
        }else{
            
            let vc = ST_EditDailyMomentController()
            
            vc.model = data_array[safe:indexPath.row]
            
            self.navigationController?.pushViewController(vc)
        }
        
        
    }
    
}



//MARK: - Cell
class SelectST_DailyMomentCell: UICollectionViewCell {
    
    static let width = (ScreenWidth - 15 * 2 - 23 * 2)/3.0
    
    static let height = SelectST_DailyMomentCell.width + 10 + 21
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(cover_root_view)
        
        cover_root_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(cover_root_view.snp.width)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cover_root_view.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(21)
        }
        
        cover_root_view.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 20, left: 20, bottom: 20, right: 20))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //    MARK: - Properties
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()
    
    
    fileprivate lazy var cover_root_view:UIView = {
        let control = UIView()
        control.cornerRadius = 15
        control.backgroundColor = UIColor.init(hex: 0xF5F8FA)
        return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textAlignment = .center
        control.textColor = Color.init(hexString: "#3A3A3A")
        control.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.regular)
        return control
    }()
    
    weak var model:ST_DailBaseModel?{
        didSet{
            
            guard let m = model else { return  }
            
            cover_image_view.pin_setImage(from: m.image.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = m.name
        }
    }
}
